package com.xds.gulimall.order.interceptor;

import com.xds.common.constant.AuthServerConstant;
import com.xds.common.vo.MemberResVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author : baixu
 * @date : 10:35 2021/12/23
 */
@Component
public class LoginUserInterceptor implements HandlerInterceptor {
    public static ThreadLocal<MemberResVo> loginUser=new ThreadLocal<>();



    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        ///order/order/status/{orderSn} 放行 无需登陆
        String uri = request.getRequestURI();
        boolean match = new AntPathMatcher().match("/order/order/status/**", uri);
        if (match){
            return true;
        }
        MemberResVo attribute = (MemberResVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        if (attribute!=null){
            //登陆
            loginUser.set(attribute);
            return true;
        }else {
            //没登陆 就去登陆
            request.getSession().setAttribute("msg","请先登陆");
            response.sendRedirect("http://auth.xiaowenplus.com/login.html");
            return false;
        }
    }
}
